﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;

namespace Onetube.Providers
{
     public static class ExtentionMethods
    {
        private static DateTime _epochDate;

        static ExtentionMethods()
        {
            ExtentionMethods._epochDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        }

        public static string ByteToString(this byte[] buffer)
        {
            string str = "";
            for (int i = 0; i < (int)buffer.Length; i++)
            {
                str = string.Concat(str, buffer[i].ToString("X2"));
            }
            return str;
        }

        public static DateTime EpocToDateTime(this double seconds)
        {
            return ExtentionMethods._epochDate.ToUniversalTime().AddSeconds(seconds);
        }

    


        public static T GetParent<T>(this DependencyObject item)
        where T : DependencyObject
        {
            if (item is T)
            {
                return (T)item;
            }
            return VisualTreeHelper.GetParent(item).GetParent<T>();
        }

        public static bool JsonValueToBool(this JToken token)
        {
            if (token == null)
            {
                return false;
            }
            return (bool)token;
        }

        public static DateTime JsonValueToDateTime(this JToken token)
        {
            if (token == null)
            {
                return DateTime.MinValue;
            }
            return (DateTime)token;
        }

        public static double JsonValueToDouble(this JToken token)
        {
            if (token == null)
            {
                return 0;
            }
            return (double)((double)token);
        }

        public static string JsonValueToEncoding(this JToken token)
        {
            if (token == null)
            {
                return null;
            }
            return token.SelectToken("url").JsonValueToString();
        }

        public static int JsonValueToInt(this JToken token)
        {
            if (token == null)
            {
                return 0;
            }
            return (int)token;
        }

        public static long JsonValueToLong(this JToken token)
        {
            if (token != null)
            {
                return (long)token;
            }
            return (long)0;
        }

        public static string JsonValueToString(this JToken token)
        {
            if (token == null)
            {
                return string.Empty;
            }
            return (string)token;
        }

        public static Dictionary<string, string> ParseQueryString(this string s)
        {
            Dictionary<string, string> strs = new Dictionary<string, string>();
            if (s.Contains("?"))
            {
                s = s.Substring(s.IndexOf('?') + 1);
            }
            string[] strArrays = Regex.Split(s, "&");
            for (int i = 0; i < (int)strArrays.Length; i++)
            {
                string[] strArrays1 = Regex.Split(strArrays[i], "=");
                if ((int)strArrays1.Length != 2)
                {
                    strs.Add(strArrays1[0], string.Empty);
                }
                else
                {
                    strs.Add(strArrays1[0], strArrays1[1]);
                }
            }
            return strs;
        }

        public static string RemoveAllSpecialCharacters(this string inputString)
        {
            if (string.IsNullOrEmpty(inputString))
            {
                return string.Empty;
            }
            return (new Regex("(?:[^a-z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)).Replace(inputString, string.Empty);
        }

        public static string RemoveSpecialCharacters(this string inputString)
        {
            if (string.IsNullOrEmpty(inputString))
            {
                return string.Empty;
            }
            return inputString.Replace("\\", "");
        }

        public static string ReversedSubString(this string value, int start, int end)
        {
            if (string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }
            return new string(((IEnumerable<char>)value.Substring(start + 1, end - start).ToCharArray()).Reverse<char>().ToArray<char>());
        }

        public static JToken SelectToken(this JToken token, string key)
        {
            if (token == null || string.IsNullOrEmpty(key))
            {
                return null;
            }
            string str = key;
            string[] strArrays = new string[] { "." };
            string[] strArrays1 = str.Split(strArrays, StringSplitOptions.None);
            for (int i = 0; i < (int)strArrays1.Length; i++)
            {
                string str1 = strArrays1[i];
                token = token[key];
            }
            return token;
        }

        public static string Slice(this string source, int start, int end)
        {
            if (end < 0)
            {
                end = source.Length + end;
            }
            return source.Substring(start, end - start);
        }

        public static double ToEpocSeconds(this DateTime dateTime)
        {
            return (dateTime.ToUniversalTime() - ExtentionMethods._epochDate).TotalSeconds;
        }

        public static string ToTitleCase(this string str)
        {
            string str1 = str;
            if (!string.IsNullOrEmpty(str))
            {
                string[] strArrays = str.Split(new char[] { ' ' });
                for (int i = 0; i < (int)strArrays.Length; i++)
                {
                    string str2 = strArrays[i];
                    if (str2.Length > 0)
                    {
                        char chr = str2[0];
                        strArrays[i] = string.Concat(chr.ToString().ToUpper(), str2.Substring(1));
                    }
                }
                str1 = string.Join(" ", strArrays);
            }
            return str1;
        }

        public static string Truncate(this string value, int maxLength)
        {
            if (string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }
            if (value.Length <= maxLength)
            {
                return value;
            }
            return value.Substring(0, maxLength);
        }
    }
}
